from django.shortcuts import redirect
from django.views.generic.simple import direct_to_template
from django import forms
from django.http import HttpResponseRedirect, HttpResponse
from eurobet.app.models import Game, UserProfile, Team, Prediction
from django.contrib.auth.forms import UserCreationForm
from django.core.context_processors import csrf
from django.contrib import auth
from django.contrib.auth.models import User

def index(request):
    userAuthenticated = False
    if request.user.is_authenticated():
        userAuthenticated = True
        user = request.user;
    
    # completed_games = Game.objects.filter(is_completed=True)
    # playoff_games = Game.objects.filter(is_playoff=True)   
    
    games = Game.objects.all().order_by('date')
    teams = Team.objects.all()
    user_points = 2
    user_rank = 25
    
    return direct_to_template(request, "index.html", locals())

def sign_in(request):
    if request.method == 'POST':
        email = request.POST.get('email_input', '')
        password = request.POST.get('pass_input', '')
        
        if email != "" and password != "":
            user = auth.authenticate(username=email, password=password)
            if user is not None and user.is_active:
                auth.login(request, user)
                return redirect('/')
            else:
                # add error message to context and return result
                return redirect('/')
        else:
            # add error message to context and return result
            return redirect('/')
    return redirect('/')

def sign_out(request):
    auth.logout(request)
    return redirect('/')

def register(request):
    if request.method == 'POST':
        fn = request.POST.get('fn_input', '')
        ln = request.POST.get('ln_input', '')
        email = request.POST.get('email_input', '')
        password1 = request.POST.get('pass_input', '')
        password2 = request.POST.get('retype-pass-input', '')
        
        if fn != "" and ln != "" and email != "" and password1 != "" and password1 != password2:
            user = User.objects.create_user(email, email, password1)
            user.first_name = fn
            user.last_name = ln
            user.save()
            return redirect('/')
    else:
        return direct_to_template(request, "register.html")

def score(request):
    scores = 1 #UserProfile.objects.all().order_by('points').reverse()
    return direct_to_template(request,"score.html", locals())

def table(request):
    games = Game.objects.all();
   # current_time = datetime.now
   # left_time = 
    return direct_to_template(request, "table.html", locals())

def points(request):
    return direct_to_template(request, "points.html")

def rank(request):
    return direct_to_template(request, "rank.html")

def all_passed_games(request):
    games = Game.objects.all();
    return direct_to_template(request, "table.html", locals())

def all_following_games(request):
    games = Game.objects.all();
    return direct_to_template(request, "table.html", locals())

def xhr_add_game_prediction(request):
    if request.is_ajax():
        user = request.user
        action = request.POST.get('action')
        element_id = request.POST.get('id')
        value = request.POST.get('value')
        
        game_id = element_id.split('-')[1]
        game = Game.objects.get(pk=game_id)
        game_team = element_id.split('-')[2]
        
        prediction = Prediction.objects.get(user=user, game=game)
        if prediction:
            if game_team == 'home':
                prediction.home_team_bet = value
            else:
                prediction.guest_team_bet = value
        else:
            if game_team == 'home':
                prediction = Prediction.objects.create(user=user, game=game, home_team_bet=value)
            else:
                prediction = Prediction.objects.create(user=user, game=game, guest_team_bet=value)
        
        prediction.save()
        message = "added"
    else:
        message = 'failed'
    return HttpResponse(message)

def xhr_cards_prediction(request):
    if request.is_ajax():
        a = 1
    return HttpResponse('added')
        